﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Threading;
using System.Web;
using FaceSharp.Api.GraphApi;

namespace FaceSharp.Api.Objects
{
    public class Feed : GraphBase
    {
        public Feed(IFacebookCore facebookCore): base(facebookCore)
        {

        }


        /// <summary>
        /// http://developers.facebook.com/docs/reference/api/post
        /// </summary>
        /// <returns>
        /// Should Return the following URL if successful:
        /// https://graph.facebook.com/[toUserid]/feed
        /// (where [toUserid] is the toUserid paramater passed in.
        /// </returns>
        /// <param name="toUserId"></param>
        /// <param name="message">The message</param>
        /// <param name="pictureUrl">If available, a link to the picture included with this post</param>
        /// <param name="link">The link attached to this post</param>
        /// <param name="name">The name of the link</param>
        /// <param name="caption">The caption of the link (appears beneath the link name)</param>
        /// <param name="description">A description of the link (appears beneath the link caption)</param>
        /// <param name="source">If available, the source link attached to this post (for example, a flash or video file)</param>
        public string Publish(string toUserId, string message, string pictureUrl, string link, string name, string caption, string description, string source = null)
        {
            Trace.Write("Begin Request", "GraphApi.FeedPublish");

            RestSharp.RestClient client;
            RestSharp.RestRequest request;
            GenericGraphRequest(
                toUserId + "/feed", 
                new Dictionary<string, string>()
                    {
                        {"message", message},
                        {"picture", pictureUrl},
                        {"link", link},
                        {"name", name},
                        {"caption", caption},
                        {"description", description},
                        {"source",source}
                    },
                out client, out request,isPost:true);
            var response = client.Execute(request);
            if(response.StatusCode != HttpStatusCode.OK)
            {
                throw new HttpException((int)response.StatusCode,"GraphApi.FeedPublish Failed:" + response.Content);
            }
            var returnVal = response.Content;
            Trace.Write("End Request", "GraphApi.FeedPublish");
            return returnVal;
        }
    }
}